package bustime;

import java.util.List;

import com.andro.chansung.R;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class BusDetailAdapter extends BaseAdapter {

	private List<BusDetail> buses;
	private LayoutInflater inflater;
	private Activity activity;
	private ViewHolder viewHolder;
	
	public BusDetailAdapter(Activity activity, List<BusDetail> buses) {
		this.activity = activity;
		this.buses = buses;
		this.inflater = LayoutInflater.from(activity);
	}
	
	private class ViewHolder {
		public TextView timeTv;
		public TextView numberTv;
	}
	
	public int getCount() {
		return buses.size();
	}

	public BusDetail getItem(int position) {
		return buses.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		
		if( view == null ) {
			viewHolder = new ViewHolder();
			view = inflater.inflate(R.layout.bus_detail_how_cell_layout, null);
			viewHolder.timeTv = (TextView)view.findViewById(R.id.bus_detail_show_cell_time_tv);
			viewHolder.numberTv = (TextView)view.findViewById(R.id.bus_detail_show_cell_number_tv);
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder)view.getTag();
		}
		
		if( getCount() == 0 ) {
			viewHolder.timeTv.setTag("제공되지 않습니다.");
		} else {
			BusDetail bus = getItem(position);
			viewHolder.timeTv.setText(bus.getTime());
			viewHolder.numberTv.setText(bus.getNumber() + " 대");
		}
		
		return view;
	}

}
